<template>
  <Dialog :title="dialogTitle" v-model="dialogVisible" width="60%">
    <el-form
      ref="formRef"
      :model="formData"
      :rules="formRules"
      label-width="120px"
      v-loading="formLoading"
    >

      <el-row :gutter="20">
        <el-col :span="12">
      <el-form-item label="车辆名称" prop="name">
        <el-input v-model="formData.name" placeholder="请输入车辆名称" />
      </el-form-item>
        </el-col>

        <el-col :span="12">
      <el-form-item label="车牌号码" prop="plateNumber">
        <el-input v-model="formData.plateNumber" placeholder="请输入车牌号码" />
      </el-form-item>
        </el-col>
      </el-row>



      <el-row :gutter="20">
        <el-col :span="12">
      <el-form-item label="车身颜色" prop="color">
        <el-input v-model="formData.color" placeholder="请输入车身颜色" />
      </el-form-item>
        </el-col>

        <el-col :span="12">

      <el-form-item label="座位数" prop="seatCount">
        <el-input v-model="formData.seatCount" placeholder="请输入座位数" />
      </el-form-item>
        </el-col>
      </el-row>


      <el-row :gutter="20">
        <el-col :span="12">
      <el-form-item label="发动机号" prop="engineNumber">
        <el-input v-model="formData.engineNumber" placeholder="请输入发动机号" />
      </el-form-item>
        </el-col>

        <el-col :span="12">
      <el-form-item label="车架号" prop="vin">
        <el-input v-model="formData.vin" placeholder="请输入车架号(VIN)" />
      </el-form-item>

        </el-col>
      </el-row>

      <el-row :gutter="20">
<!--        <el-col :span="12">-->
<!--          <el-form-item label="车辆油耗" prop="fuelConsumption">-->
<!--            <el-input v-model="formData.fuelConsumption" placeholder="请输入油耗" />-->
<!--          </el-form-item>-->

<!--        </el-col>-->

        <el-col :span="12">
          <el-form-item label="初始里程数" prop="initialMileage">
            <el-input v-model="formData.initialMileage" placeholder="请输入初始里程数（公里）" />
          </el-form-item>
        </el-col>

<!--        <el-col :span="12">-->
<!--          <el-form-item label="驾驶员" prop="driverId">-->
<!--            <el-select v-model="formData.driverId" placeholder="请选择驾驶员" filterable>-->
<!--              <el-option-->
<!--                v-for="user in users"-->
<!--                :key="user.id"-->
<!--                :label="user.nickname"-->
<!--                :value="user.nickname"-->
<!--              />-->
<!--            </el-select>-->
<!--          </el-form-item>-->
<!--        </el-col>-->

        <!-- <el-col :span="12">
          <el-form-item label="行驶证车主" prop="driverId">
            <el-input v-model="formData.driverName" placeholder="请输入行驶证车主" />
          </el-form-item>
        </el-col> -->
      </el-row>

      <el-row :gutter="20">
        <el-col :span="12">
          <el-form-item label="车审到期日期" prop="inspectionExpiryDate">
            <el-date-picker
              v-model="formData.inspectionExpiryDate"
              type="date"
              value-format="x"
              placeholder="选择车审到期日期"
            />
          </el-form-item>

        </el-col>

        <el-col :span="12">
          <el-form-item label="保险到期日期" prop="insuranceExpiryDate">
            <el-date-picker
              v-model="formData.insuranceExpiryDate"
              type="date"
              value-format="x"
              placeholder="选择保险到期日期"
            />
          </el-form-item>
        </el-col>
      </el-row>


      <el-row :gutter="20">


<!--        <el-col :span="12">-->
<!--          <el-form-item label="购买价格" prop="purchasePrice">-->
<!--            <el-input v-model="formData.purchasePrice" placeholder="请输入购买价格" />-->
<!--          </el-form-item>-->
<!--        </el-col>-->

        <el-col :span="12">
          <el-form-item label="购买日期" prop="purchaseDate">
            <el-date-picker
              v-model="formData.purchaseDate"
              type="date"
              value-format="x"
              placeholder="选择购买日期"
            />
          </el-form-item>
        </el-col>
      </el-row>




      <!-- 相关附件 -->
      <el-col :span="24">
        <el-form-item label="相关附件" >
          <OaFileManager :folderDisabled="true" :initial-file-ids="formData.files" @fileSelectSuccess="fileSelectScccess"  />
        </el-form-item>
      </el-col>


      <el-form-item label="备注信息" prop="remark">
        <el-input v-model="formData.remark"  style="width: 100%;" :rows="4" type="textarea" placeholder="请输入备注信息" />
      </el-form-item>
    </el-form>
    <template #footer>
      <el-button @click="submitForm" type="primary" :disabled="formLoading">确 定</el-button>
      <el-button @click="dialogVisible = false">取 消</el-button>
    </template>
  </Dialog>
</template>

<script setup lang="ts">
import { VehicleApi, VehicleVO } from '@/api/oa/vehicle'
import * as UserApi from '@/api/system/user'
import { ref, reactive, defineEmits, defineExpose } from 'vue'
import { useI18n } from 'vue-i18n'
import UserSelect from '@/views/system/user/UserSelect.vue'

// 其他响应式变量保持不变
const users = ref([]) // 用于存储用户列表

const { t } = useI18n() // 国际化
const message = useMessage() // 消息弹窗

const dialogVisible = ref(false) // 弹窗的是否展示
const dialogTitle = ref('') // 弹窗的标题
const formLoading = ref(false) // 表单的加载中：1）修改时的数据加载；2）提交的按钮禁用
const formType = ref('') // 表单的类型：create - 新增；update - 修改
const formData = ref({
  name: undefined,
  plateNumber: undefined,
  purchaseDate: undefined,
  purchasePrice: undefined,
  color: undefined,
  seatCount: undefined,
  fuelConsumption: undefined,
  engineNumber: undefined,
  vin: undefined,
  inspectionExpiryDate: undefined,
  insuranceExpiryDate: undefined,
  initialMileage: undefined,
  driverName: undefined,
  remark: undefined
})
const formRules = reactive({
  name: [{ required: true, message: '车辆名称不能为空', trigger: 'blur' }],
  plateNumber: [{ required: true, message: '车牌号码不能为空', trigger: 'blur' }],
  color: [{ required: true, message: '车身颜色不能为空', trigger: 'blur' }],
  engineNumber: [{ required: true, message: '发动机号不能为空', trigger: 'blur' }],
  vin: [{ required: true, message: '车架号(VIN)不能为空', trigger: 'blur' }],
  inspectionExpiryDate: [{ required: true, message: '车审到期日期不能为空', trigger: 'blur' }],
  insuranceExpiryDate: [{ required: true, message: '保险到期日期不能为空', trigger: 'blur' }],
})
const formRef = ref() // 表单 Ref


const fileSelectScccess = (fileIds: string[]) => {
  formData.value.files = fileIds.join(',')
}

/** 打开弹窗 */
const open = async (type: string, id?: number) => {
  dialogVisible.value = true
  dialogTitle.value = t('action.' + type)
  formType.value = type
  resetForm()

  // 获取用户列表

  // 修改时，设置数据
  if (id) {
    formLoading.value = true
    try {
      formData.value = await VehicleApi.getVehicle(id)
    } finally {
      formLoading.value = false
    }
  }
}
defineExpose({ open }) // 提供 open 方法，用于打开弹窗

/** 提交表单 */
const emit = defineEmits(['success']) // 定义 success 事件，用于操作成功后的回调
const submitForm = async () => {
  // 校验表单
  await formRef.value.validate()
  // 提交请求
  formLoading.value = true
  try {
    const data = formData.value as unknown as VehicleVO
    if (formType.value === 'create') {
      await VehicleApi.createVehicle(data)
      message.success(t('common.createSuccess'))
    } else {
      await VehicleApi.updateVehicle(data)
      message.success(t('common.updateSuccess'))
    }
    dialogVisible.value = false
    // 发送操作成功的事件
    emit('success')
  } finally {
    formLoading.value = false
  }
}
/** 员工被点击事件 */
const handleNodeChange = async (row) =>{
  formData.value.handlerId = row.id
  formData.value.nickName = row.nickname
}

/** 重置表单 */
const resetForm = () => {
  formData.value = {
    name: undefined,
    plateNumber: undefined,
    purchaseDate: undefined,
    purchasePrice: undefined,
    color: undefined,
    seatCount: undefined,
    fuelConsumption: undefined,
    engineNumber: undefined,
    vin: undefined,
    inspectionExpiryDate: undefined,
    insuranceExpiryDate: undefined,
    initialMileage: undefined,
    driverName: undefined,
    remark: undefined
  }
  formRef.value?.resetFields()
}
</script>
